package com.jcfk.eam.service.base;


import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.enums.DeleteFlag;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.eam.dao.base.EamInspectionRouteDao;
import com.jcfk.eam.domain.dto.base.EamInspectionRouteDTO;
import com.jcfk.eam.domain.dto.base.EamInspectionRouteLocationDTO;
import com.jcfk.eam.domain.po.base.EamInspectionRoute;
import com.jcfk.eam.domain.po.base.EamInspectionRouteLocation;
import com.jcfk.eam.domain.query.base.EamInspectionRouteQuery;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.jcfk.common.core.utils.BeanUtils;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.security.utils.SecurityUtils;
import com.alibaba.druid.util.StringUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

import java.util.List;

import java.util.Date;

/**
 * 巡检路线
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-08-27
 */
@Service
public class EamInspectionRouteService extends BaseService<EamInspectionRouteDao, EamInspectionRoute> {


    @Autowired
    private EamInspectionRouteLocationService eamInspectionRouteLocationService;

    /**
     * EamInspectionRoute分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamInspectionRouteDTO> queryByPage(DataPage<EamInspectionRouteDTO> page, EamInspectionRouteQuery query) {
        // 分页查询
        query.setRouteName(StringUtils.isEmpty(query.getRouteName()) ? query.getRouteName() : query.getRouteName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }

    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamInspectionRouteDTO>
     */
    public List<EamInspectionRouteDTO> getList(EamInspectionRouteQuery query) {
        // 查询条件
        QueryWrapper<EamInspectionRoute> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getRouteCode()), EamInspectionRoute::getRouteCode, query.getRouteCode());
        qw.lambda().like(!StrUtil.isEmpty(query.getRouteName()), EamInspectionRoute::getRouteName, query.getRouteName());
        qw.lambda().eq(EamInspectionRoute::getEnabled, AvailableStatus.ENABLE.getValue());
        qw.lambda().eq(EamInspectionRoute::getDeleted, DeleteFlag.NOTDELETED.getValue());
        // 查询数据
        List<EamInspectionRoute> list = super.list(qw);

        return convertList(list, EamInspectionRouteDTO.class);
    }

    /**
     * 获取根据关联id详情表列表
     *
     * @param query 查询条件
     * @return List<EamInspectionRouteDTO>
     */
    public List<EamInspectionRouteLocationDTO> getDetails(EamInspectionRouteQuery query) {
        // 查询条件
        QueryWrapper<EamInspectionRouteLocation> qw = new QueryWrapper<>();
        qw.lambda().eq(!StrUtil.isEmpty(query.getId()), EamInspectionRouteLocation::getRouteId, query.getId());

        // 查询数据
        List<EamInspectionRouteLocation> list = eamInspectionRouteLocationService.list(qw);

        return convertList(list, EamInspectionRouteLocationDTO.class);
    }


    /**
     * 判断EamInspectionRoute是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamInspectionRouteDTO dto) {
        QueryWrapper<EamInspectionRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", 0);
        return RetResult.ok();
    }

    /**
     * 插入巡检路线
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamInspectionRouteDTO dto) {

        // 保存
        EamInspectionRoute item = new EamInspectionRoute();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);

        // 设置关联主键
        for (EamInspectionRouteLocationDTO detail : dto.getDetails()) {
            detail.setRouteId(item.getId());
        }

        // 保存明细表
        eamInspectionRouteLocationService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 更新巡检路线
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamInspectionRouteDTO dto) {

        // 保存
        EamInspectionRoute item = new EamInspectionRoute();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);

        // 设置关联主键
        for (EamInspectionRouteLocationDTO detail : dto.getDetails()) {
            detail.setRouteId(item.getId());
        }
        // 保存明细表
        eamInspectionRouteLocationService.saveBatch(dto.getDetails(), item.getId());
    }


    /**
     * 根据批量主键删除巡检路线(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamInspectionRouteDTO dto) {

        EamInspectionRoute item = new EamInspectionRoute();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamInspectionRoute> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamInspectionRoute item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());
        item.setLastUpdatedUserName(SecurityUtils.getUserName());
    }


    /**
     * 根据主键获取巡检路线信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamInspectionRouteDTO get(String id) {
        // 查询数据
        EamInspectionRoute item = this.baseMapper.selectById(id);
        EamInspectionRouteDTO dto = new EamInspectionRouteDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据主键获取巡检路线信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteitem(String id) {
        EamInspectionRoute item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }
}

